﻿using Mapster;
using Minotaur.App.Core;
using Minotaur.App.Domains.Dto;
using Prism.Commands;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Minotaur.App.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Application";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        private readonly ModuleCatalogManager moduleCatalogManager;
        public readonly IModuleManager moduleManager;
        private readonly IRegionManager _regionManager;
        private readonly GlobalUserModel globalUserModel;
        private double menuStackPaneHeight;

        public MainWindowViewModel(IModuleManager moduleManager, GlobalUserModel globalUserModel, IRegionManager regionManager, ModuleCatalogManager moduleCatalogManager)
        {
            this.moduleManager = moduleManager;
            this.globalUserModel = globalUserModel;
            this._regionManager = regionManager;
            this.moduleCatalogManager = moduleCatalogManager;
        }

        /// <summary>
        /// 最大化按钮
        /// </summary>
        public Button BtnMaxWindow { get; set; }
        /// <summary>
        /// 最小化按钮
        /// </summary>
        public Button BtnMinWindow { get; set; }
        public ObservableCollection<MenuInfoDto> MenuInfos => this.globalUserModel.MenuInfos.Adapt<ObservableCollection<MenuInfoDto>>();



        /// <summary>
        /// 关闭事件
        /// </summary>
        public ICommand CloseComand { get => new DelegateCommand(CloseAction); }

        /// <summary>
        /// 放大事件
        /// </summary>
        public ICommand MaxComand { get => new DelegateCommand(MaxAction); }

        private void MaxAction()
        {
            Application.Current.MainWindow.WindowState = WindowState.Maximized;
            this.BtnMinWindow.Visibility = Visibility.Visible;
            this.BtnMaxWindow.Visibility = Visibility.Collapsed;
            this.MenuStackPaneHeight = CurrentWindow.ActualHeight - 90;
        }

        /// <summary>
        /// 缩小事件
        /// </summary>
        public ICommand MinComand { get => new DelegateCommand(MinAction); }

        private void MinAction()
        {
            Application.Current.MainWindow.WindowState = WindowState.Normal;
            this.BtnMaxWindow.Visibility = Visibility.Visible;
            this.BtnMinWindow.Visibility = Visibility.Collapsed;
            this.MenuStackPaneHeight = CurrentWindow.ActualHeight - 80;
        }

        /// <summary>c.
        /// 隐藏事件
        /// </summary>
        public ICommand HiddenComand { get => new DelegateCommand(HiddenAction); }

        private void HiddenAction()
        {
            Application.Current.MainWindow.WindowState = WindowState.Minimized;
        }

        public Window CurrentWindow { get; set; }
        public double MenuStackPaneHeight { get => menuStackPaneHeight; set => SetProperty(ref menuStackPaneHeight, value); }

        public void Initialization(Button btnMin, Button btnMax, Window window, MaterialDesignThemes.Wpf.ColorZone colorBackGroud, Modules.SystemControl.Views.SideMenu menuInfo)
        {
            this.BtnMinWindow = btnMin;
            this.BtnMaxWindow = btnMax;
            this.CurrentWindow = window;
            colorBackGroud.MouseLeftButtonDown += MainWindow_MouseLeftButtonDown;
            colorBackGroud.MouseDoubleClick += MainWindow_MouseDoubleClick;
            this.MenuStackPaneHeight = Application.Current.MainWindow.ActualHeight - 80;
            this.moduleCatalogManager.InitModules();
            menuInfo.ItemsSource = null;
            menuInfo.OpendMenuClick -= OpendMenuClick;
            menuInfo.ItemsSource = MenuInfos;
            menuInfo.OpendMenuClick += OpendMenuClick;
        }

        private void OpendMenuClick(object sender, RoutedEventArgs e)
        {
            MenuInfoDto menuInfoDto = e.OriginalSource as MenuInfoDto;
            OpendMenuInfo(menuInfoDto);
        }

        private void MainWindow_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                this.CurrentWindow.DragMove();

            }
            catch
            {
            }

        }

        private void MainWindow_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {

            if (this.CurrentWindow.WindowState == WindowState.Normal)
            {
                this.CurrentWindow.WindowState = WindowState.Maximized;
                this.BtnMaxWindow.Visibility = Visibility.Collapsed;
                this.BtnMinWindow.Visibility = Visibility.Visible;
            }
            else
            {
                this.CurrentWindow.WindowState = WindowState.Normal;
                this.BtnMaxWindow.Visibility = Visibility.Visible;
                this.BtnMinWindow.Visibility = Visibility.Collapsed;
            }
            this.MenuStackPaneHeight = Application.Current.MainWindow.ActualHeight - 80;
        }

        private void CloseAction()
        {
            System.Diagnostics.ProcessStartInfo process = new ProcessStartInfo();
            process.FileName = Process.GetCurrentProcess().MainModule.FileName; //EXEAdress为exe程序所在的位置
            process.UseShellExecute = false;
            System.Diagnostics.Process.Start(process);
            Application.Current.Shutdown();
        }

        internal void OpendMenuInfo(MenuInfoDto menuInfo)
        {
            if (menuInfo != null && (menuInfo.Childs is null || !menuInfo.Childs.Any()))
            {
                if (this.moduleCatalogManager.OpendModule(menuInfo.ModuleCode) && GlobalConst.Views.Any(o => o == menuInfo?.Code))
                {
                    _regionManager?.RequestNavigate(RegionNames.ContentRegion, menuInfo?.Code);
                }
                else
                {
                    this.moduleCatalogManager.OpendModule("system");
                    _regionManager?.RequestNavigate(RegionNames.ContentRegion, "404");
                }
            }
        }
    }
}
